package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Override
    @Transactional
    public void add(Pet pet) {
        super.add(pet);
        //添加宠物的同时要添加宠物详情
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        //判断是否有详情
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        //删除宠物的同时删除详情
        petDetailMapper.delete(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        for(Long id : ids){
            delete(id);
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            //这里直接更新是因为之前已经在分页查询到id了
            petDetailMapper.update(petDetail);
            //在petdetail.xml里删除id=#{id} 在修改的时候容易把详情中的pet_id修改为null，导致下一次回显失败。所以在修改宠物详情的时候不要修改宠物id
        }
    }

    //上架
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id:ids){
            Pet pet = petMapper.findById(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            //判断
            if(boo1 && boo2){
                //上架并添加时间

                petMapper.onsale(id,new Date());
                //修改状态
                log.setState(1);
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);
            }else{
                log.setState(0);
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败");
            }

        }
        return new AjaxResult();
    }
    //下架
    @Override
    public AjaxResult offsale(List<Long> ids) {

        petMapper.offsale(ids,new Date());

        return new AjaxResult();
    }


}
